package com.hpin.fansea.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.MyRoomNumberAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.ApproveResult;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * 认证选择小区期次页面
 * Created by Aaron on 15/9/21.
 */
public class CommunityIssueActivity extends BaseActivity {

//    private String cityId;
    private String communityId;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_community_issue);

//        cityId = getIntent().getStringExtra("cityId");
        communityId = getIntent().getStringExtra("communityId");

        initWidget();

        // 获取期次
        getIssueFromNet();

    }

    /**
     * 获取小区期次
     */
    private void getIssueFromNet() {
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("communityId", communityId);

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.COMMUNITYISSUE, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ApproveResult approveResult = JSONObject.parseObject(str, ApproveResult.class);

                    if (Constant.STATUS_SUCESS.equals(approveResult.status)) {

                        initData(approveResult.result);

                    }else {
                        showToast(approveResult.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("获取数据失败");
                }

            }
        }, null);

    }

    protected void initData(final List<ApproveResult.ApproveDetail> approveList) {

        if (approveList==null || approveList.size()==0)
            return;

        approveNameList = new ArrayList<String>();

        for (ApproveResult.ApproveDetail approveDetail : approveList) {
            approveNameList.add(approveDetail.projectName);
        }
        adapter = new MyRoomNumberAdapter(approveNameList, this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                String projectId = approveList.get(position).projectId;
                Intent intent = new Intent(CommunityIssueActivity.this, MyRoomNumberActivity.class);
                intent.putExtra("projectId", projectId);
                intent.putExtra("projectName", approveList.get(position).projectName);
                startActivity(intent);

            }
        });
    }

    private ArrayList<String> approveNameList;

    private MyRoomNumberAdapter adapter;

    private void initWidget() {

        listView = (ListView) findViewById(R.id.my_roomnumber_list);

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.my_issue));

        iv_left.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();
                break;

        }

    }
}
